package com.wa.config;

import cn.hutool.extra.spring.SpringUtil;
import com.corundumstudio.socketio.SocketIONamespace;
import com.wa.constants.NameSpaceBeanNameConstant;
import com.wa.constants.WebSocketEventConstant;
import com.wa.handler.BomNamespaceMessageEventHandler;
import com.wa.handler.BomSaasNamespaceMessageEventHandler;
import com.wa.handler.WebSocketCommonMessageEventHandler;
import com.wa.handler.WebSocketConnectHandler;
import com.wa.vo.BomWebSocketPackage;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
 * @author recharde.li@allchips.com
 * @version V1.0
 * @date 2020-05-28 13:16
 **/
//@Configuration
//@ConditionalOnBean(value = WebSocketNameSpaceConfig.class)
public class WebSocketEventConfig {

    /**
     * webSocketEventHandler
     */
    private final WebSocketConnectHandler webSocketConnectHandler;

    /**
     * bomNamespaceMessageEventHandler
     */
    private BomNamespaceMessageEventHandler bomNamespaceMessageEventHandler;

    /**
     * bomSaasNamespaceMessageEventHandler
     */
    private BomSaasNamespaceMessageEventHandler bomSaasNamespaceMessageEventHandler;

    /**
     * webSocketCommonMessageEventHandler
     */
    private final WebSocketCommonMessageEventHandler<Object> webSocketCommonMessageEventHandler;

    @Autowired
    public WebSocketEventConfig(WebSocketConnectHandler webSocketConnectHandler, BomNamespaceMessageEventHandler bomNamespaceMessageEventHandler, BomSaasNamespaceMessageEventHandler bomSaasNamespaceMessageEventHandler, WebSocketCommonMessageEventHandler<Object> webSocketCommonMessageEventHandler) {
        this.webSocketConnectHandler = webSocketConnectHandler;
        this.bomNamespaceMessageEventHandler = bomNamespaceMessageEventHandler;
        this.webSocketCommonMessageEventHandler = webSocketCommonMessageEventHandler;
        this.bomSaasNamespaceMessageEventHandler = bomSaasNamespaceMessageEventHandler;
    }

    /**
     * 添加各种事件监听
     */
    @PostConstruct
    @SuppressWarnings({"raw"})
    public void initBomSocketIONamespace() {
        SocketIONamespace bomSocketIONamespace = SpringUtil.getBean(NameSpaceBeanNameConstant.BOM);
        bomSocketIONamespace.addConnectListener(webSocketConnectHandler);
        bomSocketIONamespace.addDisconnectListener(webSocketConnectHandler);
        bomSocketIONamespace.addEventInterceptor(webSocketConnectHandler);
        bomSocketIONamespace.addEventListener(WebSocketEventConstant.SEND_EVENT_CODE, BomWebSocketPackage.class, bomNamespaceMessageEventHandler);
        bomSocketIONamespace.addEventListener(WebSocketEventConstant.MESSAGE_EVENT, BomWebSocketPackage.class, bomNamespaceMessageEventHandler);
    }

    @PostConstruct
    public void initWebSiteSocketIONamespace() {
        SocketIONamespace websiteSocketIONamespace = SpringUtil.getBean(NameSpaceBeanNameConstant.WEBSITE);
        websiteSocketIONamespace.addConnectListener(webSocketConnectHandler);
        websiteSocketIONamespace.addDisconnectListener(webSocketConnectHandler);
        websiteSocketIONamespace.addEventInterceptor(webSocketConnectHandler);
        websiteSocketIONamespace.addEventListener(WebSocketEventConstant.SEND_EVENT_CODE, Object.class, webSocketCommonMessageEventHandler);
    }

    @PostConstruct
    public void initBossSocketIONamespace() {
        SocketIONamespace bossSocketIONamespace = SpringUtil.getBean(NameSpaceBeanNameConstant.BOSS);
        bossSocketIONamespace.addConnectListener(webSocketConnectHandler);
        bossSocketIONamespace.addDisconnectListener(webSocketConnectHandler);
        bossSocketIONamespace.addEventInterceptor(webSocketConnectHandler);
        bossSocketIONamespace.addEventListener(WebSocketEventConstant.SEND_EVENT_CODE, Object.class, webSocketCommonMessageEventHandler);
    }

    @PostConstruct
    public void initProductSocketIONamespace() {
        SocketIONamespace productSocketIONamespace = SpringUtil.getBean(NameSpaceBeanNameConstant.PRODUCT);
        productSocketIONamespace.addConnectListener(webSocketConnectHandler);
        productSocketIONamespace.addDisconnectListener(webSocketConnectHandler);
        productSocketIONamespace.addEventInterceptor(webSocketConnectHandler);
        productSocketIONamespace.addEventListener(WebSocketEventConstant.SEND_EVENT_CODE, Object.class, webSocketCommonMessageEventHandler);
    }

    @PostConstruct
    public void initBomSaasSocketIONamespace() {
        SocketIONamespace bomSaasSocketIONamespace = SpringUtil.getBean(NameSpaceBeanNameConstant.BOM_SAAS);
        bomSaasSocketIONamespace.addConnectListener(webSocketConnectHandler);
        bomSaasSocketIONamespace.addDisconnectListener(webSocketConnectHandler);
        bomSaasSocketIONamespace.addEventInterceptor(webSocketConnectHandler);
        bomSaasSocketIONamespace.addEventListener(WebSocketEventConstant.SEND_EVENT_CODE, BomWebSocketPackage.class, bomSaasNamespaceMessageEventHandler);
        bomSaasSocketIONamespace.addEventListener(WebSocketEventConstant.MESSAGE_EVENT, BomWebSocketPackage.class, bomSaasNamespaceMessageEventHandler);
    }

}
